package Candidate;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

import com.sun.mail.smtp.SMTPTransport;


public class SendEmail
{

	public SendEmail(){

	}
	
	public boolean sendEmail(CandidateModel cModel) {
		boolean status = false;
				Properties props = new Properties();
				final String  d_email = cModel.getFrom();
				final String d_password = cModel.getPassEmail();
				String d_host = "smtp.gmail.com", d_port  = "465", m_to = cModel.getTo();
				
				props.put("mail.smtp.host", "smtp.gmail.com");
				props.put("mail.smtp.port", d_port);
				props.put("mail.smtp.starttls.enable","true");
				props.put("mail.smtp.debug", "true");
				props.put("mail.smtp.auth", "true");
				props.put("mail.smtp.socketFactory.port",d_port);
				props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
				props.put("mail.smtp.socketFactory.fallback", "false");
		            Session session = Session.getInstance(props,
		                        new javax.mail.Authenticator() {
		                             protected PasswordAuthentication getPasswordAuthentication()
		                             {
		                                   return new PasswordAuthentication(d_email,d_password);
		                             }
		                        });
		            
		            session.setDebug(true);
	      
	     	MimeMessage message= new MimeMessage(session);
	      
	      try {
			message.setFrom(new InternetAddress(d_email));
			
			message.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
			
			message.setSubject(cModel.getSubject());
			
			message.setContent(cModel.getContent(),"text/html");
			
			
			Transport transport = session.getTransport("smtps");
			transport.connect(d_host, Integer.parseInt(d_port),  d_email, d_password);
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
			
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return status;
	}
	
}

